package com.ruoyi.project.ledger.controller;


import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.ledger.domain.LedgerManagementRegistration;
import com.ruoyi.project.ledger.domain.LedgerWorker;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.ledger.domain.LedgerSafetyEducation;
import com.ruoyi.project.ledger.service.ILedgerSafetyEducationService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 安全教育活动台账Controller
 *
 * @author gwc
 * @date 2020-04-14
 */
@RestController
@RequestMapping("/ledger/education")
public class LedgerSafetyEducationController extends BaseController
{
    @Autowired
    private ILedgerSafetyEducationService ledgerSafetyEducationService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询安全教育活动台账列表
     */
//    @PreAuthorize("@ss.hasPermi('ledger:education:list')")
    @GetMapping("/list")
    public TableDataInfo list(LedgerSafetyEducation ledgerSafetyEducation)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            ledgerSafetyEducation.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        ledgerSafetyEducation.setAreas(areas);
        ledgerSafetyEducation.setManagementCharacters(managementCharacters);
        startPage();
        List<LedgerSafetyEducation> list = ledgerSafetyEducationService.selectLedgerSafetyEducationList(ledgerSafetyEducation);
        return getDataTable(list);
    }

    /**
     * 导出安全教育活动台账列表
     */
//    @PreAuthorize("@ss.hasPermi('ledger:education:export')")
    @Log(title = "安全教育活动台账", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LedgerSafetyEducation ledgerSafetyEducation)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            ledgerSafetyEducation.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        ledgerSafetyEducation.setAreas(areas);
        ledgerSafetyEducation.setManagementCharacters(managementCharacters);
        List<LedgerSafetyEducation> list = ledgerSafetyEducationService.selectLedgerSafetyEducationList(ledgerSafetyEducation);
        ExcelUtil<LedgerSafetyEducation> util = new ExcelUtil<LedgerSafetyEducation>(LedgerSafetyEducation.class);
        return util.exportExcel(list, "education");
    }

    /**
     * 获取安全教育活动台账详细信息
     */
    /*@PreAuthorize("@ss.hasPermi('ledger:education:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ledgerSafetyEducationService.selectLedgerSafetyEducationById(id));
    }

    /**
     * 新增安全教育活动台账
     */
    @PreAuthorize("@ss.hasPermi('ledger:education:add')")
    @Log(title = "安全教育活动台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LedgerSafetyEducation ledgerSafetyEducation)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station"))){
            Long stationId = user.getStationId();
            ledgerSafetyEducation.setStationId(stationId);
        }else{
            return AjaxResult.error("当前角色无新增权限");
        }
        ledgerSafetyEducation.setCreateId(user.getUserId().toString());
        return toAjax(ledgerSafetyEducationService.insertLedgerSafetyEducation(ledgerSafetyEducation));
    }

    /**
     * 修改安全教育活动台账
     */
    @PreAuthorize("@ss.hasPermi('ledger:education:edit')")
    @Log(title = "安全教育活动台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LedgerSafetyEducation ledgerSafetyEducation)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station"))){
            Long stationId = user.getStationId();
            ledgerSafetyEducation.setStationId(stationId);
        }else{
            return AjaxResult.error("当前角色无修改权限");
        }
        ledgerSafetyEducation.setUpdateId(user.getUserId().toString());
        return toAjax(ledgerSafetyEducationService.updateLedgerSafetyEducation(ledgerSafetyEducation));
    }

    /**
     * 删除安全教育活动台账
     */
    @PreAuthorize("@ss.hasPermi('ledger:education:remove')")
    @Log(title = "安全教育活动台账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ledgerSafetyEducationService.deleteLedgerSafetyEducationByIds(ids));
    }

    @GetMapping("/selectNoAttendWorkerList")
    public TableDataInfo selectNoAttendWorkerList(LedgerWorker ledgerWorker)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色判断
        if(permissionService.hasRole("admin")){    //管理员
        }else if(permissionService.hasRole("station") || permissionService.hasRole("hs_station")){     //场站
            Long stationId = user.getStationId();
            ledgerWorker.setStationId(stationId);
        }
        startPage();
        List<LedgerWorker> list = ledgerSafetyEducationService.selectNoAttendWorkerList(ledgerWorker);
        return getDataTable(list);
    }

    @GetMapping("/selectNoAttendees")
    public TableDataInfo selectNoAttendees(LedgerManagementRegistration managementRegistration)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色判断
        if(permissionService.hasRole("admin")){    //管理员
        }else if(permissionService.hasRole("station") || permissionService.hasRole("hs_station")){     //场站
            Long stationId = user.getStationId();
            managementRegistration.setStationId(stationId);
        }
        startPage();
        List<LedgerManagementRegistration> list = ledgerSafetyEducationService.selectNoAttendees(managementRegistration);
        return getDataTable(list);
    }

    @GetMapping("/selectAttendees")
    public TableDataInfo selectAttendees(LedgerManagementRegistration managementRegistration)
    {
        startPage();
        List<LedgerManagementRegistration> list = ledgerSafetyEducationService.selectAttendees(managementRegistration);
        return getDataTable(list);
    }

    @GetMapping("/selectAttendWorkerList")
    public TableDataInfo selectAttendWorkerList(LedgerWorker ledgerWorker)
    {
        startPage();
        List<LedgerWorker> list = ledgerSafetyEducationService.selectAttendWorkerList(ledgerWorker);
        return getDataTable(list);
    }

    /**
     * 新增参加培训人员
     */
    @PostMapping("/addAttendWorker")
    public AjaxResult addAttendWorker(@RequestBody LedgerSafetyEducation ledgerSafetyEducation)
    {
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station"))){

        }else{
            return AjaxResult.error("当前角色无权限新增");
        }
        return toAjax(ledgerSafetyEducationService.addAttendWorker(ledgerSafetyEducation));
    }

    /**
     * 删除安全教育活动台账
     */

    @DeleteMapping("/deleteAttendWorker")
    public AjaxResult deleteAttendWorker(LedgerWorker ledgerWorker)
    {
        int i = ledgerSafetyEducationService.deleteAttendWorker(ledgerWorker.getId());
        return toAjax(i);
    }

    /**
     * 导出安全教育活动台账详细信息
     */
    @Log(title = "安全教育活动台账", businessType = BusinessType.EXPORT)
    @GetMapping(value = "exportDetail/{id}")
    public AjaxResult exportDetail(@PathVariable("id") Long id)
    {
        return ledgerSafetyEducationService.exportDetail(id);
    }
}
